package com.example.cloud.controller.member;

import com.alibaba.fastjson.JSON;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.basic.ServiceProject;
import com.example.cloud.model.business.reservation.Patient;
import com.example.cloud.model.dto.PayInfo;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.finance.ChargeDetails;
import com.example.cloud.model.finance.ConsumeDiscount;
import com.example.cloud.model.finance.DeductRecord;
import com.example.cloud.model.finance.GiftRecord;
import com.example.cloud.model.finance.PaymentDetails;
import com.example.cloud.model.finance.RechargeRecord;
import com.example.cloud.model.finance.WaitPayment;
import com.example.cloud.model.member.Member;
import com.example.cloud.model.system.SystemConfig;
import com.example.cloud.service.basic.info.MemberService;
import com.example.cloud.service.system.SystemConfigService;
import com.example.cloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by wangtao on 2018/7/30.
 * 会员管理控制器
 */
@RestController
@RequestMapping("/member")
public class MemberController extends BaseController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private SystemConfigService configService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        return this.singleQuery(model, Member.class);
    }

    @GetMapping("/find/{searchValue}")
    public Result find(@PathVariable String searchValue) {
        return ResultUtil.success(memberService.findMember(searchValue));
    }

    @PostMapping("/saveOrUpdate")
    public void saveOrUpdate(@RequestBody Member record) {
        memberService.doSave(record);
    }

    @PostMapping("/openAccount")
    public Result openAccount(@RequestBody Patient patient) {
        return memberService.openAccount(patient);
    }

    /**
     * 充值
     *
     * @param record
     * @return
     */
    @PostMapping("/recharge")
    public Result recharge(@RequestBody RechargeRecord record) {
        // 调用充值逻辑
        return memberService.doRecharge(record);
    }

    /**
     * 赠送
     *
     * @param record
     * @return
     */
    @PostMapping("/gift")
    public Result gift(@RequestBody GiftRecord record) {
        return memberService.doGift(record);
    }

    /**
     * 付款
     *
     * @param map
     * @return
     */
    @PostMapping("/payment")
    public Result payment(@RequestBody Map map) {
        return memberService.doPayment(JSON.parseArray(map.get("chargeDetails").toString(), ChargeDetails.class),
                JSON.parseArray(map.get("discounts").toString(), ConsumeDiscount.class), JSON.parseObject(map.get("payInfo").toString(), PayInfo.class),
                JSON.parseObject(map.get("payment").toString(), PaymentDetails.class), JSON.parseArray(map.get("serviceIds").toString(), Long.class));
    }

    @PostMapping("/addService/{patientId}/{memberId}")
    public void saveByServiceProject(@RequestBody List<ServiceProject> records, @PathVariable Long patientId, @PathVariable Long memberId) {
        memberService.doAddService(records, patientId, memberId);
    }

    /**
     * 划扣
     *
     * @param records
     * @return
     */
    @PostMapping("/deduct")
    public Result deduct(@RequestBody List<DeductRecord> records) {
        return memberService.doDeduct(records);
    }

    /**
     * 补交
     *
     * @param map
     * @return
     */
    @PostMapping("/make")
    public Result make(@RequestBody Map map) {
        // 补交处理逻辑
        return memberService.doMake(JSON.parseArray(map.get("records").toString(), WaitPayment.class),
                JSON.parseObject(map.get("payInfo").toString(), PayInfo.class),
                JSON.parseObject(map.get("payment").toString(), PaymentDetails.class));
    }

    public int checkPrint() {
        int autoPrint = 0;
        SystemConfig systemConfig = configService.findOne();
        if (systemConfig != null) {
            autoPrint = systemConfig.getAutoPrint();
        }
        return autoPrint;
    }

}
